package cn.wisenergy.pai.monitor.linux.parser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;


public class TimeNode implements Serializable{
	private static final long serialVersionUID = 3262397337150977111L;
	//主机信息
	private HostSpot hostspot;
	//时间
	private Time time;
	//cpu使用率
	private CPU cpu;
	//内存
	private Memory mem;
	//磁盘读取速率，写入速率
	private Disk disk;
	//网络接受、发送速率
	private NetWork nets;
	
	public HostSpot getHostspot() {
		return hostspot;
	}
	public void setHostspot(HostSpot hostspot) {
		this.hostspot = hostspot;
	}
	public Time getTime() {
		return time;
	}
	public void setTime(Time time) {
		this.time = time;
	}
	public CPU getCpu() {
		return cpu;
	}
	public void setCpu(CPU cpu_all) {
		this.cpu = cpu_all;
	}
	public Memory getMem() {
		return mem;
	}
	public void setMem(Memory mem) {
		this.mem = mem;
	}
	public Disk getDisk() {
		return disk;
	}
	public void setDisk(Disk disk) {
		this.disk = disk;
	}
	public NetWork getNets() {
		return nets;
	}
	public void setNets(NetWork nets) {
		this.nets = nets;
	}
	@Override
	public String toString() {
		return "TimeNode [\n" +
				"\t hostname= " + hostspot.getHostName() + ",\n " +
				"\t hostip  = " + hostspot.getIp() + ",\n " +
				"\t time    = " + time + ",\n" +
				"\t cpu_use = " + cpu.getCpu()*100 + "%,\n" +
				
				"\t mem_use   = " + mem.getUsedMem()*100 + "%,\n" +
				"\t mem_sum   = " + mem.getTotalMem() + "KB,\n" +
				"\t mem_free  = " + mem.getFreeMem() + "KB,\n" +
				"\t mem_avaliable  = " + mem.getAvaliableMem() + "KB,\n" +
				"\t mem_swapfree   = " + mem.getSwapFree() + "KB,\n" +
				"\t mem_swaptotal  = " + mem.getSwapTotal() + "KB,\n" +
				
				"\t disk_read    = " + disk.getDiskread() + "KB,\n" +
				"\t disk_write   = " + disk.getDiskwrite() + "KB,\n" +
				"\t nets_accept  = " + nets.getNetaccept() + "KB,\n" +
				"\t nets_send    = " + nets.getNetsend() + "KB,\n" +
				"\t ] " + " \n" +
				"\t  ";
	}
	public void writeToStream(DataOutput stream,Serializable data) throws IOException{
		byte[] bytes=data.toBytes();
		stream.writeInt(bytes.length);
		stream.write(bytes);
	}
	public void readFromStream(DataInput stream,Serializable data)throws IOException{
		int len=stream.readInt();
		byte[] bytes=new byte[len];
		stream.readFully(bytes, 0, len);
		data.toObject(bytes);
	}
	@Override
	public byte[] toBytes() throws IOException{
		ByteArrayOutputStream bytesOutput=new ByteArrayOutputStream();
		DataOutputStream dataOutput=new DataOutputStream(bytesOutput);
		if(hostspot != null){
			writeToStream(dataOutput,hostspot);
		}
		if(time != null){
			writeToStream(dataOutput,time);
		}
		if(cpu != null){
			writeToStream(dataOutput,cpu);
		}
		if(mem != null){
			writeToStream(dataOutput,mem);
		}
		if(disk != null){
			writeToStream(dataOutput,disk);
		}
		if(nets != null){
			writeToStream(dataOutput,nets);
		}
		byte[] result=bytesOutput.toByteArray();
		dataOutput.close();
		bytesOutput.close();
		return result;
	}
	@Override
	public void toObject(byte[] ba) throws IOException {
		ByteArrayInputStream bytesInput=new ByteArrayInputStream(ba);
		DataInputStream dataInput=new DataInputStream(bytesInput);
		if(dataInput.available()>0){
			hostspot=new HostSpot();
			readFromStream(dataInput,hostspot);
		}
		if(dataInput.available()>0){
			time=new Time();
			readFromStream(dataInput,time);
		}
		if(dataInput.available()>0){
			cpu=new CPU();
			readFromStream(dataInput,cpu);
		}
		if(dataInput.available()>0){
			mem=new Memory();
			readFromStream(dataInput,mem);
		}
		if(dataInput.available()>0){
			disk=new Disk();
			readFromStream(dataInput,disk);
		}
		if(dataInput.available()>0){
			nets=new NetWork();
			readFromStream(dataInput,nets);
		}
		dataInput.close();
		bytesInput.close();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((hostspot == null) ? 0 : hostspot.hashCode());
		result = prime * result + ((time == null) ? 0 : time.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TimeNode other = (TimeNode) obj;
		if (hostspot == null) {
			if (other.hostspot != null)
				return false;
		} else if (!hostspot.equals(other.hostspot))
			return false;
		if (time == null) {
			if (other.time != null)
				return false;
		} else if (!time.equals(other.time))
			return false;
		return true;
	}
}